#!/usr/bin/ruby
# -*- ruby -*-
#
# http://programmingpraxis.com/2013/09/06/cartesian-product/

require 'pp'

def _cart_rec(arrays, current_array = 0, result = [], results = [])
  if current_array >= arrays.size
    results << result
  else
    arrays[current_array].each do |current_element|
      _cart_rec(arrays, current_array + 1, result.dup << current_element, results)
    end
  end

  return results
end

def cart_rec(*arrays)
  return _cart_rec(arrays)
end

a = [1, 2, 3]
b = [4]
c = [5, 6]

pp cart_rec(a, b, c)

